//
//  ClassAndStruct.swift
//  SwiftDemo
//
//  Created by lizq on 16/7/5.
//  Copyright © 2016年 lizq. All rights reserved.
//

import UIKit

struct Point {
    var x = 0.0, y = 0.0
}

struct Size {
    var width = 0.0, height = 0.0
}

struct Rect {
    var origin = Point()
    var size = Size()
    var center: Point {
        get{
            let centerX = origin.x + size.width/2
            let centerY = origin.y + size.height/2
            return Point(x: centerX, y: centerY)
        }
        set{
            
            origin.x = newValue.x - (size.width / 2)
            origin.y = newValue.y - (size.height / 2)
        }
    }
}

class StepCounter {
    var totalSteps:Int = 0 {
        willSet {
            print("about to set totalSteps to \(newValue)")
        }
        didSet {
//            if totalSteps > oldValue {
                print("added \(totalSteps - oldValue) steps")
//            }
        }
    }
}

struct Resolution {
   // class definetion goes here
    var height = 0
    var width = 0
}

struct FixedLengthRange {
    var firstValue:Int
    let length:Int
}

class VideoMode {
    var resolution = Resolution()
    var interlaced = false
    var frameRate = 0.0
    var name:String?
}

class DataImporter {
    var fileName = "data.txt"
}

class DataManager {
    lazy var importer = DataImporter()
    var data = [String]()
}




class ClassAndStruct: NSObject {
    
    func action() {
        let someResolution = Resolution()
        let someVideoMode = VideoMode()
        print("someResolution.width : \(someResolution.width)")
        print("someVideoMode.reslolution : \(someVideoMode.resolution)")
        let vga = Resolution(height: 20, width: 20)
        print("vga = \(vga)")
        var cinema = vga
        print("cinema = \(cinema)")
        cinema.width = 1080
        cinema.height = 720
        print("cinema = \(cinema)")
        print("vga = \(vga)")
        //class是引用类型 结构体和枚举是值类型
        let videoMode1 = VideoMode()
        videoMode1.resolution.height = 100
        let videoMode2 = videoMode1
        print("videoMode1 = \(videoMode1.resolution)")
        print("videoMode2 = \(videoMode2.resolution)")
        print("after")
        videoMode2.resolution.width = 200
        print("videoMode1 = \(videoMode1.resolution)")
        print("videoMode2 = \(videoMode2.resolution)")
        
        if(videoMode2 === videoMode1) {
            print("videoMode2 and VideoMode1 refer to the some resolution instance")
        }
        let string1 = "hello"
        var string2 = string1
        print("string1 = \(string1)")
        print("string2 = \(string2)")
        string2 = "world"
        print("after")
        print("string1 = \(string1)")
        print("string2 = \(string2)")
        
        var rangeOfThreeItems = FixedLengthRange(firstValue: 0, length: 3)
        rangeOfThreeItems.firstValue = 6
//        rangeOfThreeItems.length = 6 // error
        
        let range = FixedLengthRange(firstValue: 6, length: 6)
//        range.firstValue = 8 //error
        /*
         *说明：当值类型的实例被声明为常量的时候，它的所有属性也就成了常量。属于引用类型的
         *类（class）则不一样。把一个引用类型的实例赋给一个常量后，仍然可以修改该实例的变量属性
         */
        let manager = DataManager()
        manager.data.append("some data")
        manager.data.append("some more data")
        print(manager.importer.fileName)
        
        let steps = StepCounter()
        steps.totalSteps = 200
        steps.totalSteps = 400
        steps.totalSteps = 800
        steps.totalSteps = 800
    }
    
}
